package com.lenovo.manufacture.module;

import android.os.Bundle;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.annotation.Layout;

import butterknife.BindView;
import butterknife.OnClick;

public class Module1 extends BaseActivity {

    @BindView(R.id.iv_status)
    ImageView ivStatus;
    @BindView(R.id.btn_open)
    BootstrapButton btnOpen;
    @BindView(R.id.btn_close)
    BootstrapButton btnClose;

    @Layout(value = R.layout.activity_module1, title = "工厂灯光")
    protected void init() {
        async(apiService.getInfo(param.add("id", 1).build()),
                data -> setImageResource(data.getData().get(0).get("isLighting")));
    }

    private void setImageResource(String isLighting) {
        int res = "0".equals(isLighting) ? R.drawable.light_off : R.drawable.light_on;
        ivStatus.setImageResource(res);
    }

    @OnClick(R.id.btn_open)
    public void onBtnOpenClicked() {
        async(apiService.updateLightSwitch(param.add("isLighting", 1).build()), data -> {
            showMsg("开灯成功");
            setImageResource("1");
        });
    }

    @OnClick(R.id.btn_close)
    public void onBtnCloseClicked() {
        async(apiService.updateLightSwitch(param.add("isLighting", 0).build()), data -> {
            showMsg("关灯成功");
            setImageResource("0");
        });
    }
}
